<template>
  <div class="tenantMenu_main main_container">
    <div class="main_header">
      <div class="padding_container">
        <div class="left">
          <Breadcrumb />
        </div>
        <div class="right">
          <HeaderOperation :parent="this" />
        </div>
      </div>
    </div>
    <div v-loading="loading" class="main_main">
      <search-bar-new :parent="this" />
      <div ref="table_container" class="table_container">
        <el-table
          ref="table"
          :data="tableData"
          stripe
          border
          :height="tableHeight"

          :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
          :cell-style="cellStyle"
          @cell-click="cellClick"
          @sort-change="(column)=>{sortChange(column, this)}"
          @selection-change="(val)=>{selectionChange(val, this)}"
        >
          <el-table-column type="selection" width="70" align="center" />
          <Column v-for="(column,index) in tableColumns" :key="index" :prop="column.prop" :is-show="column.isShow" :label="column.label" :sortable="column.sortable" :width="column.width" :min-width="column.minWidth" />
          <el-table-column label="操作" align="center" fixed="right" width="180">
            <template slot-scope="scope">
              <el-button
                v-if="scope.row.status==1||(scope.row.status==0&&!scope.row.planId)"
                v-hasPermi="['device:checkRecord:operate']"
                type="text"
                @click.stop="check(scope.row)"
              >点检</el-button>
              <span v-else class="el-button--text button_grey">点检</span>
              <el-button
                v-if="((scope.row.status==1||scope.row.status==2)&&scope.row.planId)||(!scope.row.planId&&scope.row.status !==3)"
                v-hasPermi="['device:checkRecord:edit']"
                type="text"
                @click.stop="edit(scope.row)"
              >编辑</el-button>
              <span v-else class="el-button--text button_grey">编辑</span>
              <span v-if="scope.row.status ==3||scope.row.status ==2" class="el-button--text button_grey">删除</span>
              <el-button
                v-else
                v-hasPermi="['device:checkRecord:delete']"
                type="text"
                @click.stop="deleteOne(scope.row)"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <div class="main_bottom">

      <pagination
        :total="total"
        :page.sync="pageData.pageNum"
        :limit.sync="pageData.pageSize"
        :selected-num="selectionsAll.length"
        @pagination="getList"
      />
    </div>

  </div>
</template>

<script>
import { list, remove, getInfo } from '@/api/equipmentManage/equipmentCheck/checkRecord'
import { teamlist } from '@/api/equipmentManage/equipmentReport/checkReport'
import { getEquipmentList1 } from '@/api/equipmentManage/baseInfo/equipmentInfo'
import { mapGetters } from 'vuex'
import { getDictSelects } from '@/api/common/common'
import AddOrUpdate from './addOrUpdate' // 新增或编辑页面
import seeOrCheck from './seeOrCheck.vue'
import ShowDetailOld from './showDetail.vue' // 查看页面
export default {
  name: 'Index',
  data() {
    return {
      loading: true,
      pageId: '点检工单', // 页面唯一标识
      pageExportId: '点检工单导出', // 页面导出标识
      pageData: {
        pageNum: 1,
        pageSize: 20,
        orderByColumn: 'code',
        isAsc: 'desc'
      },
      // tableKey: 1,
      total: 1, // 数据一共有多少条
      mainId: 'id', // 表格行ID
      tableHeight: '0', // 表格高度
      tableData: [], // 表格数据
      tableColumns: [], // 表格实际用的字段
      searchColumns: [], // 搜索区实际用的字段
      selections: [], // 表格当前页所选中的行
      selectionsAll: [], // 表格所有页选中的行
      defaultTableColumns: [// 表格常规字段（初始值）
        { prop: 'statusName', label: '状态', canOrder: false, width: '100' },
        { prop: 'bookName', label: '点检设备', minWidth: '200', canOrder: false },
        { prop: 'location', label: '设备位置', minWidth: '200', canOrder: false },
        { prop: 'code', label: '点检工单号', sortable: 'custom' },
        { prop: 'planCode', label: '点检计划单号', sortable: 'custom' },
        { prop: 'planTime', label: '计划点检时间', minWidth: '140', sortable: 'custom' },
        { prop: 'baseMaintenanceName', label: '维保班组', canOrder: false },
        { prop: 'shiftLabel', label: '班次', canOrder: false },
        { prop: 'templateName', label: '点检模版', canOrder: false },
        { prop: 'checkPersonName', label: '点检人员', canOrder: false },
        { prop: 'completeTime', label: '点检完成时间', minWidth: '140', sortable: 'custom' },
        { prop: 'remark', label: '备注', canOrder: false, minWidth: '200' }

      ],
      exportTableColumns: [// 表格常规字段（初始值）
        { prop: 'statusName', label: '状态', canOrder: false, width: '100' },
        { prop: 'bookName', label: '点检设备', minWidth: '200', canOrder: false },
        { prop: 'location', label: '设备位置', minWidth: '200', canOrder: false },
        { prop: 'code', label: '点检工单号', sortable: 'custom' },
        { prop: 'planCode', label: '点检计划单号', sortable: 'custom' },
        { prop: 'planTime', label: '计划点检时间', minWidth: '140', sortable: 'custom' },
        { prop: 'baseMaintenanceName', label: '维保班组', canOrder: false },
        { prop: 'shiftLabel', label: '班次', canOrder: false },
        { prop: 'templateName', label: '点检模版', canOrder: false },
        { prop: 'checkPersonName', label: '点检人员', canOrder: false },
        { prop: 'completeTime', label: '点检完成时间', minWidth: '140', sortable: 'custom' },
        { prop: 'remark', label: '备注', canOrder: false, minWidth: '200' }
      ],
      defaultExport: [],
      defaultSearchColumns: [// 搜索区常规字段（初始值）
        { prop: 'searchValue', label: '模糊搜索', type: 'input' },
        { prop: 'bookId', label: '点检设备', type: 'select', optionsName: 'equipmentList' },
        // { prop: 'code', label: '点检工单号', type: 'input' },
        // { prop: 'planCode', label: '点检计划单号', type: 'input' },
        // { prop: 'checkPersonName', label: '点检人员', type: 'input' },
        { prop: 'shiftCode', label: '班次', type: 'dict', optionsName: 'statusOptions2' },
        { prop: 'statusList', label: '状态', type: 'select-multiple', optionsName: 'statusOptions' },
        { prop: 'planTime', label: '计划点检时间', type: 'time', isTime: true },
        { prop: 'completeTime', label: '点检完成时间', type: 'time', isTime: true },
        // { prop: 'location', label: '设备位置', type: 'input' },
        { prop: 'maintenanceId', label: '维保班组', type: 'select', optionsName: 'maintenanceList' }
        // { prop: 'baseMaintenanceName', label: '维保班组', type: 'input' },
        // { prop: 'templateName', label: '点检模版', type: 'input' }
      ],
      operationData: [ // 右上角的操作按钮
        { label: '新增', perm: 'device:checkRecord:add' },
        { label: '批量删除', perm: 'device:checkRecord:delete' },
        { label: '批量导出3', perm: '' },
        { label: '页面配置', perm: '' }
      ],
      statusOptions: [ // 选择组件数据源
        { value: 0, label: '待点检' },
        { value: 1, label: '点检中' },
        { value: 2, label: '已点检' },
        { value: 3, label: '已逾期' }
      ],
      statusOptions2: [ // 选择组件数据源
        { value: undefined, label: '全部' },
        { value: 141, label: '白班' },
        { value: 142, label: '夜班' }
      ],
      equipmentList: [],
      maintenanceList: [],
      showKey: {
        type: 'column_two',
        keys: [
          { type: 'form', name: '点检工单号', key: 'code' },
          { type: 'form', name: '点检计划单号', key: 'planCode' },
          { type: 'form', name: '点检设备', key: 'bookName' },
          { type: 'form', name: '设备位置', key: 'location' },
          { type: 'form', name: '计划点检时间', key: 'planTime' },
          { type: 'form', name: '班次', key: 'shiftLabel' },
          { type: 'form', name: '点检模板', key: 'templateName' },
          { type: 'form', name: '备注', key: 'remark', length: 'double' },
          { type: 'form', name: '点检人员', key: 'checkPersonName' },
          { type: 'form', name: '维保班组', key: 'baseMaintenanceName' },
          { type: 'form', name: '点检完成时间', key: 'completeTime' },
          { type: 'table', key: 'detailList' }
        ]
      }
    }
  },
  created() {
    this.loadEquipmentList()
    this.getDictOptions()
    // if (this.notificationId !== 0) {
    //   const rowId = { id: this.notificationId }
    //   if (this.$store.state.notification.notificationStatus == '1') {
    //     this.showDetail(rowId)
    //   } else {
    //     this.check(rowId)
    //   }
    // }
    // if (this.$store.state.order.contendId) {
    //   this.check({ id: this.$store.state.order.contendId })
    //   this.$store.state.order.contendId = ''
    // }
  },
  computed: {
    ...mapGetters([
      'notificationId', 'userName'
    ])
  },
  watch: {
    notificationId(newValue, oldValue) {
      if (newValue !== oldValue && newValue !== 0 && this.$store.state.notification.notificationStatus == '0') {
        const rowId = { id: this.notificationId }
        this.check(rowId)
      } else if (newValue !== oldValue && newValue !== 0 && this.$store.state.notification.notificationStatus == '1') {
        const rowId = { id: this.notificationId }
        this.showDetail(rowId)
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
    })
    this.loadteamlList()
    if (this.notificationId !== 0) {
      const rowId = { id: this.notificationId }
      if (this.$store.state.notification.notificationStatus == '1') {
        this.showDetail(rowId)
      } else {
        this.check(rowId)
      }
    }
    if (this.$store.state.order.contendId) {
      if (this.$store.state.order.isStatus == '1') {
        this.showDetail({ id: this.$store.state.order.contendId })
      } else if (this.$store.state.order.isStatus == '2') {
        this.check({ id: this.$store.state.order.contendId })
      }
      this.$store.state.order.contendId = ''
      this.$store.state.order.isStatus = ''
    }
  },
  methods: {
    loadteamlList() {
      teamlist().then(res => {
        res.data.forEach(item => {
          item['value'] = item.id
          item['label'] = item.name
        })
        this.maintenanceList = res.data
      })
    },
    cellStyle({ row, column }) {
      if (column.property == 'code') {
        return {
          cursor: 'pointer',
          color: '#387bfc'
        }
      }
    },
    cellClick(row, column, cell) {
      switch (column.property) {
        case 'code':
          this.showDetail(row)
          break
      }
    },
    showDetail(row) {
      getInfo(row[this.mainId]).then(res => {
        this.$layer.iframe({// 弹框
          content: {
            content: ShowDetailOld, // 传递的组件对象
            parent: this, // 当前的vue对象
            data: {
              showData: res.data,
              showKey: this.showKey
            }// props
          },
          area: ['1320px', '700px'],
          title: '查看点检工单', // 标题
          shadeClose: false // 点击遮罩是否关闭
        })
      })
      this.$store.dispatch('notification/changeId', 0)
    },
    getDictOptions() {
      getDictSelects('shiftCode').then(res => {
        this.statusOptions2 = res.data
      })
    },
    refresh() {
      this.pageData.pageNum = 1
      this.getList()
    },
    refresh2() {
      this.tableData = []
      setTimeout(() => {
        this.getList()
      }, 50)
    },
    getList() {
      list({ ...this.pageData, defaultSearch: '1' }).then(res => {
        this.tableData = res.data

        // 判断能不能删除，存入标识
        this.tableData.forEach(item => {
          if (item.status == 2 || item.status == 3) {
            item.canNotSelectDelete = true // 不能删除的行给标记
          }
        })
        this.total = res.total
        this.loading = false
        this.$nextTick(() => {
          this.tableHeight = this.$refs.table_container.offsetHeight
          this.setSelectRow(this)// 设置行选中
        })
      })
    },
    loadEquipmentList() {
      getEquipmentList1({ status: 1 }).then(res => {
        res.data.forEach(item => {
          item['value'] = item.id
          item['label'] = item.deviceName
        })
        this.equipmentList = res.data
      })
    },
    add() {
      this.$layer.iframe({// 弹框
        content: {
          content: AddOrUpdate, // 传递的组件对象
          parent: this, // 当前的vue对象
          data: {}// props
        },
        area: ['1320px', '700px'],
        title: '新增点检工单', // 标题
        shadeClose: false // 点击遮罩是否关闭
      })
    },
    edit(row) {
      this.$layer.iframe({// 弹框
        content: {
          content: AddOrUpdate, // 传递的组件对象
          parent: this, // 当前的vue对象
          data: {
            id: row[this.mainId]
          }// props
        },
        area: ['1320px', '700px'],
        title: '编辑点检工单', // 标题
        shadeClose: false // 点击遮罩是否关闭
      })
    },
    see(row) {
      this.$layer.iframe({// 弹框
        content: {
          content: seeOrCheck, // 传递的组件对象
          parent: this, // 当前的vue对象
          data: {
            id: row[this.mainId],
            isCheck: false
          }// props
        },
        area: ['1320px', '700px'],
        title: '查看点检记录', // 标题
        shadeClose: false // 点击遮罩是否关闭
      })
      // this.$store.dispatch('notification/changeId', 0)
    },
    check(row) {
      this.$layer.iframe({// 弹框
        content: {
          content: seeOrCheck, // 传递的组件对象
          parent: this, // 当前的vue对象
          data: {
            id: row[this.mainId],
            isCheck: true
          }// props
        },
        area: ['1320px', '700px'],
        title: '执行点检任务', // 标题
        shadeClose: false // 点击遮罩是否关闭
      })
      this.$store.dispatch('notification/changeId', 0)
    },
    deleteOne(row) {
      const _this = this
      this.$confirm('确定要删除吗', '删除确认', {
        confirmButtonText: '确定',
        cancelButtonText: '取消', cancelButtonClass: 'cancelConfirm',
        type: 'warning'
      }).then(function() {
        remove(row[_this.mainId]).then(res => {
          _this.$message({
            message: res.msg,
            type: 'success'
          })
          _this.selectionsAll = []
          _this.getList()
        })
      }).catch(() => {})
    },
    // 当前页的删除接口
    deleteMethod(deleteIds) {
      return remove(deleteIds)
    },
    getExportData(data) {
      return list(data)
    }
  }
}
</script>

<style lang="less" scoped>
</style>
